//
//  HWApplyGameViewController.m
//  HWPromoteProjectAPP
//
//  Created by 陈道斌 on 2020/3/3.
//  Copyright © 2020 陈道斌. All rights reserved.
//

#import "HWApplyGameViewController.h"
#import "HWApplyGameCell.h"
#import "HWApplyGameModel.h"
#import "HWGameDetailsViewController.h"


@interface HWApplyGameViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property(strong,nonatomic)UITableView *mainTabelView;
@property(strong,nonatomic)NSString *keywordStr;

@property(strong,nonatomic) NSString *p;
@property(strong,nonatomic) NSString *max_p;
@property(strong,nonatomic) NSMutableArray *dataMuArr;



@end

@implementation HWApplyGameViewController


- (UITableView *)mainTabelView {
    
    if (_mainTabelView == nil) {
        _mainTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 10+44, SCREEN_WIDTH, SCREEN_HEIGHT-10-44) style:UITableViewStylePlain];
        _mainTabelView.delegate = self;
        _mainTabelView.dataSource = self;
        _mainTabelView.backgroundColor =beijingyanSe;
        _mainTabelView.separatorStyle =  UITableViewCellSeparatorStyleNone;
        _mainTabelView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    
    }
    return _mainTabelView;
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    self.navigationController.navigationBarHidden = NO;
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupchangyongdeshuxing];
    // 申请UI
    [self setupUI];
    
   //  [self sendNetworkRequest];
     [self addrefreshControl];
    
    // 搜索数据
    [self sousuanniushuju];
     
    

}

// 搜索按钮
- (void)sousuanniushuju {
    
    
    
       UITextField *texfeild = [[UITextField alloc]initWithFrame:CGRectMake(20, 10, kScreenW-40, 44)];
       texfeild.delegate = self;
       texfeild.placeholder = @"快来搜索你喜欢的游戏";
       texfeild.font = [UIFont systemFontOfSize:14];
    
       UIView *sousuoView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 35, 32)];
        UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
       button.frame = CGRectMake(5, 0, 30, 30);
       [button setImage:[UIImage imageNamed:@"sousujiemian_ico"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"sousujiemian_ico"] forState:UIControlStateHighlighted];
        [sousuoView addSubview:button];
     
     
     
       texfeild.leftView = sousuoView;
        texfeild.leftViewMode = UITextFieldViewModeAlways;
      texfeild.layer.cornerRadius = 22;
       texfeild.layer.masksToBounds = YES;
       texfeild.backgroundColor = [UIColor whiteColor];
       texfeild.delegate = self;
       [self.view addSubview:texfeild];
    
     
//     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(popUpSearchView)];
//
 //    [texfeild addGestureRecognizer:tap];
    
    // 推广游戏
    
    
}







- (void)setupchangyongdeshuxing {
    
    self.navigationItem.title = @"申请游戏";
    self.view.backgroundColor = beijingyanSe;
    self.keywordStr = @"";
    
}

// 设置UI
- (void)setupUI {
    
    
    [self.view addSubview:self.mainTabelView];
    [self.mainTabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(54);
    }];
    
}









- (void)addrefreshControl {
   
    self.mainTabelView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
   [self.mainTabelView.mj_header beginRefreshing];
    
    self.mainTabelView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}




// 下拉刷新
- (void)underRefresh {
      self.p = @"1";
      NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
      NSString *uid =  [HWLocallyStoredData getLoginUserID];
      NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
      NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
      NSDictionary *parametes = @{      @"ios" : @"1",
                                   @"ct" : @"app",
                                   @"ac" : @"games",
                                   @"t" : tStr,
                                   @"uid":uid,
                                   @"sign" :signStrMd5,
                                   @"keyword":self.keywordStr,
                                   @"p":self.p,
                                        
                                        
                            };
            
            
                [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                      
                       [self.mainTabelView.mj_header endRefreshing];
                       [self.mainTabelView.mj_footer endRefreshing];

                      NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                      if ([codeStr intValue] == 1) {
                          
                          
                          self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                          
                          self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                          self.dataMuArr = [HWApplyGameModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
                          [self.mainTabelView reloadData];
                           NSLog(@"%@",response);
                    
                          
                           
                      } else {
                          
                        [MBProgressHUD showError:response[@"message"]];
                                        
                    
                          
                      }
                      
                 
                      
                  } failure:^(NSString *error) {
                     // [SVProgressHUD dismiss];
                      [self.mainTabelView.mj_header endRefreshing];
                      [self.mainTabelView.mj_footer endRefreshing];
                      
                      
//                       [MBProgressHUD showSuccess:@"登录失败"];
//                       [MBProgressHUD hideHUD];
                      
                      
                  }];
            
        
    
      
    
    


}





// 上拉加载

- (void)onRefresh {
    
    int p = [self.p intValue];
                   
    int max_p = [self.max_p intValue];
                   
     if (p > max_p) {
         
          [self.mainTabelView.mj_header endRefreshing];
          [self.mainTabelView.mj_footer endRefreshing];

        [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
                       
        return ;
                       
      }
    
        NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
          NSString *uid =  [HWLocallyStoredData getLoginUserID];
          NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
          NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
          NSDictionary *parametes = @{
                                    @"ios" : @"1",
                                    @"ct" : @"app",
                                    @"ac" : @"games",
                                    @"t" : tStr,
                                    @"uid":uid,
                                    @"sign" :signStrMd5,
                                    @"keyword":self.keywordStr,
                                    @"p":self.p,
                                            
                                };
                
                
                    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                          
                           [self.mainTabelView.mj_header endRefreshing];
                           [self.mainTabelView.mj_footer endRefreshing];

                          NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                          if ([codeStr intValue] == 1) {
                              
                              
                              self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                              self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                              
                              
                              NSMutableArray *mutableArr = [HWApplyGameModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
                              for (int i = 0; i < mutableArr.count; i++) {


                                  [self.dataMuArr addObject:mutableArr[i]];

                               }
                              
                              
                              [self.mainTabelView reloadData];
                         
                                NSLog(@"%@",response);
                              
                    
                              
                               
                          } else {
                              
                            [MBProgressHUD showError:response[@"message"]];
                                            
                        
                              
                          }
                          
                     
                          
                      } failure:^(NSString *error) {
                         // [SVProgressHUD dismiss];
                          [self.mainTabelView.mj_header endRefreshing];
                          [self.mainTabelView.mj_footer endRefreshing];
                          
                          
    //                       [MBProgressHUD showSuccess:@"登录失败"];
    //                       [MBProgressHUD hideHUD];
                          
                          
                      }];
                
            
    
    
    
        
                          
    
}











- (void)sendNetworkRequest {
    
    
    
     
         NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
         NSString *uid =  [HWLocallyStoredData getLoginUserID];
         NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
         NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
         NSDictionary *parametes = @{      @"ios" : @"1",
                                      @"ct" : @"app",
                                        @"ac" : @"games",
                                        @"t" : tStr,
                                        @"uid":uid,
                                        @"sign" :signStrMd5,
                                        @"keyword":self.keywordStr
                                        };
        
        
            [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                  
                 

                  NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                  if ([codeStr intValue] == 1) {
                 
                         NSLog(@"%@",response);
                      
                      
                      
                   
                      
                      
                      
                       
                  } else {
                      
                    [MBProgressHUD showError:response[@"message"]];
                                    
                
                      
                  }
                  
             
                  
              } failure:^(NSString *error) {
                 // [SVProgressHUD dismiss];
                  
                   [MBProgressHUD showSuccess:@"登录失败"];
                  [MBProgressHUD hideHUD];
                  
                  
              }];
        
    
    
    
    
    
    
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    
    return  self.dataMuArr.count;
    
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    
    HWApplyGameCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        
        cell = [[NSBundle mainBundle]loadNibNamed:@"HWApplyGameCell" owner:nil options:nil].firstObject;
        
    }
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    HWApplyGameModel *model =   self.dataMuArr[indexPath.row];
    cell.model = model;
    return  cell;
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return  139;
    
    
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    
    HWGameDetailsViewController *xianqing = [[HWGameDetailsViewController alloc]init];
    HWApplyGameModel *model =   self.dataMuArr[indexPath.row];
    xianqing.idStr = model.id;
    [self.navigationController pushViewController:xianqing animated:YES];
    
    
    
}




- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    
    if ([textField.text isEqualToString:@""]) {
        
        self.keywordStr = [NSString stringWithFormat:@"%@",@""];
        NSLog(@"打印 %@", self.keywordStr);
        [self.mainTabelView.mj_header beginRefreshing];
        
    } else {
        
        self.keywordStr = [NSString stringWithFormat:@"%@",textField.text];
        NSLog(@"打印 %@", self.keywordStr);
        [self.mainTabelView.mj_header beginRefreshing];
        
        
    }
    
    
    [textField resignFirstResponder];
    
    
    
    return  YES;
}




@end
